package com.me.forum.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.enums.RespType;
import com.me.forum.domain.Notice;
import com.me.forum.request.NoticeRequest;
import com.me.forum.service.impl.NoticeService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/notice")
public class AdminNoticeController {
    //by LY
    @Autowired
    private NoticeService noticeService;

    @GetMapping("/{id}")
    public IModel getById(@PathVariable String id, IModel model) {
        Notice notice = noticeService.findById(id);
        model.put("success", notice != null);
        model.put("notice", notice);
        return model;
    }

    @GetMapping
    public IModel getAllNotice(@RequestBody NoticeRequest nreq, IModel model, String page, String size){
        IPage<Notice> notices = noticeService.getAll(nreq,page,size);
        model.success(notices!= null && notices.getSize() != 0);
        model.add(RespType.Users,notices);
        return model;
    }

    @PostMapping
    public IModel addNotice(@RequestBody NoticeRequest nreq,IModel model) {
        model.success(noticeService.addNotice(nreq));
        return model;
    }

    @PutMapping
    public IModel noticeUpdate(@RequestBody NoticeRequest preq,IModel model) {
        model.success(noticeService.updateByNotice(preq));
        return model;
    }

    @DeleteMapping
    private IModel NoticeDelete(@PathVariable String id,IModel model) {
        model.success(noticeService.delete(id));
        return model;
    }
}
